//
//  PublishDetailViewController.m
//  RENTVISITOR
//
//  Created by wonder on 2017/9/26.
//  Copyright © 2017年 UNN. All rights reserved.
//

#import "PublishDetailViewController.h"
#import "RentVieitor.h"
#import "TypeCell.h"

@interface PublishDetailViewController ()<UITextViewDelegate>

@end

@implementation PublishDetailViewController {
    
    BOOL isfirst;
}

- (void)viewWillAppear:(BOOL)animated {
    
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.hidden = NO;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.title = self.titleName;
    self.view.backgroundColor = BackgroundColor;
    
    [self creatLeftView];
    [self creatView];
}

- (void)returnDetailBlock:(SelectDetailBlock)block {
    
    self.selecDetailBlock = block;
}

- (void)creatLeftView {
    
    // 自定义导航栏左侧按钮
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(-5, 0, 10, 15);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"箭头左"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    // 自定义导航栏左侧按钮
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    rightBtn.frame = CGRectMake(0, 0, 50, 20);
    [rightBtn setTitle:@"完成" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor colorWithRed:99/255.0 green:99/255.0 blue:99/255.0 alpha:1] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(doneAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)creatView {
    
   
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(5,5,KWidth-10,200)];
    textView.tag = 100;
    // 设置预设文本
    if (self.detail == nil || [self.detail isEqualToString:@""]) {
        textView.text = @"请填写房源详细描述";
        isfirst = YES;
    }else {
        textView.text = self.detail;
        isfirst = NO;
    }
    
    // 设置文本字体
    textView.font = [UIFont systemFontOfSize:13];
    // 设置文本颜色
    textView.textColor = [Utils colorConvertFromString:@"#666666"];
    // 设置文本框背景颜色
    textView.backgroundColor = [UIColor whiteColor];
    // 设置文本对齐方式
    textView.textAlignment = NSTextAlignmentLeft;
    // 设置自动纠错方式
    textView.autocorrectionType = UITextAutocorrectionTypeNo;
    
    textView.delegate = self;
    [self.view addSubview:textView];
    
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
    
    if (isfirst == YES) {
        
        textView.text = @"";
        isfirst = NO;
        
    }
    
}

- (void)backAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)doneAction {
    
    UITextView *textView = (UITextView *)[self.view viewWithTag:100];
    if (textView.text == nil || [textView.text isEqualToString:@""] || isfirst == YES) {
        
        [APPDELEGATE showPro:@"请输入描述"];
        return;
    }
    
    if (self.selecDetailBlock != nil) {
        self.selecDetailBlock(textView.text);
    }
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];
}
@end
